const TOKEN_NAME = `access_token`;

/**
 * 存储用户信息(localStorage)
 */
interface StorageState {
  [key: string]: string;
}
export const storageUserInfo = (params: StorageState) => {
  Object.keys(params).forEach(key => {
    window.localStorage[key] = params[key];
  });
};

/**
 * 读取存储信息
 */
export const getStorageInfo = (key: string) => {
  const value = window.localStorage.getItem(key);
  return value || '';
};

/**
 * 退出登录
 */
export const layout = () => {
  const params = {};
  params[TOKEN_NAME] = '';
  storageUserInfo(params);
  window.location.href = '/login';
};

/**
 *  校验用户是否未登录 （token失效/或者从未登录）
 */
export const checkUserStatus = () => {
  // const expire_time = Number(getStorageInfo('expire_time'));
  const access_token = getStorageInfo(TOKEN_NAME);
  if (!access_token) layout();
};

export default '';
